package com.zlt.bkparking.controller.salesman;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.github.pagehelper.PageInfo;
import com.zlt.bkparking.dto.AdQueryParams;
import com.zlt.bkparking.dto.ExportAdvertisementPlayedExcel;
import com.zlt.bkparking.entity.AdvertisementPlayed;
import com.zlt.bkparking.mapper.AdvertisementPlayedMapper;
import com.zlt.bkparking.service.AdvertisementPlayedService;
import com.zlt.bkparking.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: hzj
 * @Description:
 */
@RestController
@RequestMapping("ap")
public class AdvertisementPlayedController {
    @Autowired
    private AdvertisementPlayedService advertisementPlayedService;

    @Autowired
    private AdvertisementPlayedMapper advertisementPlayedMapper;

    @RequestMapping("page")
    public Result page(@RequestBody AdQueryParams params){
        PageInfo<AdvertisementPlayed> page = advertisementPlayedService.page(params);
        //TODO 下拉框数据

        return Result.ok("查询成功",page);
    }

    @RequestMapping("out")
    public void outData(HttpServletResponse response, @RequestBody AdQueryParams params){

        List<AdvertisementPlayed> list = advertisementPlayedMapper.selectPage(params.getKey(), params.getStartDate(), params.getEndDate());

        List<ExportAdvertisementPlayedExcel> collect = list.stream().map(o -> {
            ExportAdvertisementPlayedExcel excel = new ExportAdvertisementPlayedExcel();
            excel.setAdName(o.getAdvertisement().getAdName());
            excel.setEId(o.geteId());
            excel.setApStart(o.getApStart());
            excel.setSpEnd(o.getApEnd());
            excel.setAuCompany(o.getUser().getAuCompany());
            return excel;
        }).collect(Collectors.toList());

        // 定义excel的页签
        List<ExportAdvertisementPlayedExcel> excels = new ArrayList<>();

        // 字节数组输出流
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        // 构建写入对象
        ExcelWriter excelWriter = EasyExcel.write(outputStream).build();

        // 这里可以多个WriteSheet对象
        WriteSheet user = EasyExcel.writerSheet(0, "播放记录").head(ExportAdvertisementPlayedExcel.class).build();

        // 这里可以写多个write
        excelWriter.write(collect, user);

        // 写入完成
        excelWriter.finish();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());
        String fileName = format + "播放记录.xlsx";

        try {
            outputStream.writeTo(response.getOutputStream());
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("utf-8");
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"; filename*=utf-8''%s", fileName, fileName));
            response.setContentLengthLong(outputStream.size());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
